package com.example.robertluozizhao.framecollectdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import com.example.robertluozizhao.framecollectdemo.R;
import com.unionpay.UPPayAssistEx;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Lzz on 3/28/2018.
 */

public class PayActivity extends AppCompatActivity implements Response.Listener<String>, Response.ErrorListener {
    @BindView(R.id.btn_yin_lian)
    Button mBtnYinLian;
    @BindView(R.id.btn_alipay)
    Button mBtnAlipay;
    @BindView(R.id.btn_weixin)
    Button mBtnWeixin;

    public static final int PLUGIN_VALID = 0;
    public static final int PLUGIN_NOT_INSTALLED = -1;
    public static final int PLUGIN_NEED_UPGRADE = 2;
    /*****************************************************************
     * mMode参数解释： "00" - 启动银联正式环境 "01" - 连接银联测试环境
     *****************************************************************/
    private final String mMode = "01";
    private String TAG = "pay";
    private final String TN_URL_01 = "http://101.231.204.84:8091/sim/getacptn";//测试账号  // ？一般公司账号



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_yin_lian, R.id.btn_alipay, R.id.btn_weixin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_yin_lian:
                askYinLian();
                break;
            case R.id.btn_alipay:
                break;
            case R.id.btn_weixin:
                break;
        }
    }

    //   * 步骤1：从网络开始,获取交易流水号即TN
//   * 步骤2：通过银联工具类启动支付插件
//   * 步骤3：处理银联手机支付控件返回的支付结果
    private void askYinLian() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(TN_URL_01, this, this);// ？开发一搬用00
        requestQueue.add(stringRequest);
    }

    @Override
    public void onResponse(String response) {
        Log.d(TAG,response);
//        UPPayAssistEx.startSEPay(this,null,null,response,TN_URL_01,"25");
        UPPayAssistEx.startPay(this,null,null,response,mMode);
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d(TAG,error.getMessage());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Bundle extras = data.getExtras();
        String pay_result = extras.getString("pay_result");
        Log.d(TAG,"pay_result"+pay_result);
        if ("sucess".equals(pay_result)) {
            Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
        }else if ("fail".equals(pay_result)){
            Toast.makeText(this, "支付失败", Toast.LENGTH_SHORT).show();
        }else if ("cancel".equals(pay_result)){
            Toast.makeText(this, "支付取消", Toast.LENGTH_SHORT).show();
        }

    }
}
